<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/22
 * Time: 10:46
 */

namespace app\index\model;


use think\Model;

class WxMenu extends Model
{
    public function menu(){
        $menu = $this->where('pid',0)->where('state',1)->order('sort_order asc')->field('menu_id,name,pid,type,value,sort_order')->select();
        $sort = array_column($menu,'sort_order');
        array_multisort($sort,SORT_ASC,$menu);
        if(count($menu)>0){
            foreach ($menu as $key=>&$val){
                $val['sub_button']=$this->menu_time($val['menu_id']);
            }
        }
        return $menu;
    }

    public function menu_time($id){
        $sub_button=[];
        $res = self::where('pid',$id)->select();
        if(count($res)>=1){
            foreach ($res as $value){
                 $sub['type'] =$value['type'];
                 $sub['name'] =$value['name'];
                 $sub['key'] =$value['value'];
                 $sub['sort_order'] =$value['sort_order'];
                array_push( $sub_button,$sub);
            }
        }
        $sort = array_column($sub_button,'sort_order');
        array_multisort($sort,SORT_ASC,$sub_button);
        return $sub_button;
    }
}